//
//  SettingViewController.m
//  Grammar
//
//  Created by Rick on 6/25/13.
//
//

#import "SettingViewController.h"
#import "LoginViewController.h"
#import "AppDelegate.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/sdkdef.h>
#import <TencentOpenAPI/TencentOAuthObject.h>
#import "SinaWeibo.h"
#import "WebViewController.h"
#import "SynsViewController.h"
#import "DataCacheManager.h"
#import "FeedBackViewController.h"
#import "AppDelegate.h"


@interface SettingViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate,TencentSessionDelegate,SinaWeiboDelegate>
{
    NSMutableArray *_settingArray;
}
@property (retain, nonatomic) IBOutlet UITableView *settingTableView;

@end

@implementation SettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"setting" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
    _settingArray = [[NSMutableArray alloc] initWithArray:array];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate shareAppdelegate].homeTabbarController showTabBarAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_settingArray release];
    [_settingTableView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setSettingTableView:nil];
    [super viewDidUnload];
}



#pragma mark - UITableView
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 42;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_settingArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if(section == 1) {
        return 2;
    }
    return 1;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifer = @"SettingCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer] autorelease];
    }
    cell.textLabel.text = [[[_settingArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
    if (indexPath.section == 0 && indexPath.row == 0) {
        NSString *uid = (NSString*)[[DataCacheManager sharedManager] getCachedObjectByKey:KEY_USER_ID];
        if (uid && uid.length > 0) {
            SynsViewController *synsViewController = [[SynsViewController alloc] initWithNibName:@"SynsViewController" bundle:nil];
            [self.navigationController pushViewController:synsViewController animated:YES];
            [synsViewController release];
        }else {
            LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
            [self.navigationController pushViewController:loginViewController animated:YES];
            [loginViewController release];
        }
        
     }
    
    if (indexPath.section == 0 && indexPath.row == 1) {
        LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
        [self.navigationController pushViewController:loginViewController animated:YES];
        [loginViewController release];
    }
    
    if (indexPath.section == 2) {
        WebViewController *viewController = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil];
        if (indexPath.row == 0) {
            viewController.type = WebviewContentTypeHelpUse;
        }else {
            viewController.type = WebviewContentTypeRelief;
        }
        
        [self.navigationController pushViewController:viewController animated:YES];
        [viewController release];
    }*/
    if (indexPath.section == 0 && indexPath.row == 0) {
        FeedBackViewController *feedbackViewController = [[FeedBackViewController alloc] initWithNibName:@"FeedBackViewController" bundle:nil];
        [self.navigationController pushViewController:feedbackViewController animated:YES];
        [feedbackViewController release];
    }
    if (indexPath.section == 1) {
        WebViewController *viewController = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil];
        if (indexPath.row == 0) {
            viewController.type = WebviewContentTypeHelpUse;
        }else {
            viewController.type = WebviewContentTypeRelief;
        }
        
        [self.navigationController pushViewController:viewController animated:YES];
        [viewController release];
    }
    [self.settingTableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
